home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1994…tember: Reference Library / Dev.CD Sep 94.toast / Periodicals / develop / develop Issue 7 / develop 7 code / QuickTime / SimpleInMovies / SimpleInPicts.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-10-15  |  8.7 KB  |  307 lines  |  [TEXT/MPS ]

  1. /*     
  2. SimpleInMovies
  3.  
  4. Sample programs demonstrating how to open and display
  5. QuickTime™ Movies.
  6.  
  7. SimpleInPicts.c file contains the code for a couple of procedures that take
  8. care of obtaining and displaying PICTs from a movie.
  9.  
  10. Guillermo A. Ortiz
  11. Macintosh Developer Technical Support
  12. */
  13.  
  14. /* Changes log:
  15.     08/07/91 -- GetMediaHandlerDescription was not happy when receiving a track instead of a media.
  16.     12/03/91 -- Changed the track picture stuff to make it work with final version.
  17.     12/03/91 -- Changed the routine that used to show a picture of the current time, now
  18.                 it sets the poster if non is present.
  19.                 
  20.     09/15/92 -- Added DoGetMoviePicts as a sample on how to get the frames of a movie.
  21. */
  22. #include <SimpleInMovie.h>
  23.  
  24. #define pictDialog     130
  25. #define aButton        2
  26. #define usrItem        3
  27. #define txtItem        4
  28. #define noPosterAlert 131
  29.  
  30. extern void PrintThis( PicHandle );
  31.  
  32. void DoGet1MoviePict(void);
  33. void DoGetTrackPicts(void);
  34. void DoGetMoviePicts(void);
  35. extern Boolean IsAppWindow(WindowPtr);
  36.  
  37. /* User Item proc, displays a picture in the dialog. */
  38. pascal void myDrawPict(theDialog, itemNumber)
  39. WindowPtr     theDialog;
  40. short        itemNumber;
  41. {
  42. short itemType;
  43. Handle DItem;
  44. Rect      box;
  45. PicHandle theP;
  46.  
  47.     GetDItem(theDialog,itemNumber,&itemType, &DItem, &box);
  48.     if (itemType == userItem) {
  49.       theP = (PicHandle)GetWRefCon(theDialog);
  50.       DrawPicture(theP, &box);
  51.     }
  52. }
  53.  
  54. /* This routine gets the pict representing the current movie time and
  55.    displays it in a dialog. The user can then select if the frame
  56.    should be used as the movie poster.
  57. */
  58. void DoGet1MoviePict()
  59. {
  60. WindowPtr window;
  61. DialogPtr theDialog;
  62. PicHandle moviePict;
  63. short itemType, itemHit;
  64. Handle DItem;
  65. Rect box;
  66. CGrafPtr savePort;
  67. GDHandle saveGDevice;
  68. DocRecHandle    wHndl;
  69. TimeValue moovTime;
  70. TimeRecord moovTRec;
  71. OSErr err;
  72.  
  73.     if (window = FrontWindow()) { /* don't bother if no movies */
  74.       if (IsAppWindow(window) && (wHndl = (DocRecHandle)GetWRefCon(window)) ) {
  75.         GetGWorld(&savePort, &saveGDevice);
  76.         
  77.         theDialog = GetNewDialog(pictDialog,nil, (WindowPtr) -1);
  78.         SetPort(theDialog);
  79.         GetDItem(theDialog,usrItem,&itemType, &DItem, &box);
  80.         if (itemType == userItem) {
  81.           SetDItem(theDialog,usrItem,itemType, (Handle)&myDrawPict, &box);
  82.         }
  83.  
  84.         moovTime = GetMovieTime((*wHndl)->wMovie, &moovTRec);
  85.         moviePict = GetMoviePict((*wHndl)->wMovie,moovTime);
  86.         SetWRefCon(theDialog, (long)moviePict);
  87.         do {
  88.           ModalDialog(nil, &itemHit);
  89.         } while (itemHit != okButton && itemHit != cancelButton);
  90.  
  91.         
  92.         if (itemHit == okButton) { /* set the poster time */
  93.           SetMoviePosterTime((*wHndl)->wMovie, moovTime);
  94.           if (err = GetMoviesError() )
  95.             DebugStr("\perror trying to set poster time"); 
  96.         }
  97.  
  98.         CloseDialog(theDialog);
  99.         KillPicture(moviePict);
  100.         SetGWorld(savePort, saveGDevice);
  101.       }
  102.     }
  103.     return;    
  104. }
  105.  
  106. /* This routine opens a dialog and displays in succession the
  107.    frames of the first video track found in a movie. If the movie
  108.    has no video tracks then the call just returns.
  109.    
  110.    The process used is the following:
  111.    
  112.    1- Get a video track
  113.    2- Get begining and end of track
  114.    3- Call GetTrackNextInterestingTime to scan frames
  115.    4- if time = trackEnd duration time trackBegin
  116.    5- go back to 3 unless ok is clicked
  117.    
  118.    
  119. */
  120.  
  121. #define forwardNormalSpeed 0x00010000        /* normal speed (1) in fixed */
  122. void DoGetTrackPicts()
  123. {
  124. WindowPtr window;
  125. DialogPtr theDialog;
  126. PicHandle moviePict;
  127. short itemType, itemHit;
  128. Handle DItem;
  129. Rect box;
  130. CGrafPtr savePort;
  131. GDHandle saveGDevice;
  132. DocRecHandle    wHndl;
  133. TimeValue inTime, trackEnd, trackOffset;
  134. long trackCount, count;
  135. Track videoTrack = nil;
  136. Str255 creator;
  137. OSType mediaType, manuf;
  138.  
  139.     if (window = FrontWindow()) { /* don't bother if no movies */
  140.       if (IsAppWindow(window) && (wHndl = (DocRecHandle)GetWRefCon(window)) ) {
  141.         GetGWorld(&savePort, &saveGDevice);
  142.  
  143.         trackCount = GetMovieTrackCount((*wHndl)->wMovie); /* how many tracks are there? */
  144.         for (count = 1; count <= trackCount; count++) {
  145.           videoTrack = GetMovieIndTrack((*wHndl)->wMovie,count);
  146.  
  147. /**** The following does not work anymore
  148.           GetMediaHandlerDescription((Media) videoTrack,&mediaType, &creator,&manuf);
  149.       so I am changing it to make it work                                                  */
  150.           GetMediaHandlerDescription(GetTrackMedia(videoTrack),&mediaType, &creator,&manuf);
  151.  
  152.           if (mediaType == 'vide') /* type we are looking for */
  153.             break;
  154.           else
  155.             videoTrack = nil;
  156.         }
  157.         
  158.         if (!videoTrack) return; /* looped through tracks with no luck */
  159.         
  160.         theDialog = GetNewDialog(pictDialog,nil, (WindowPtr) -1);
  161.         SetPort(theDialog);
  162.  
  163.         GetDItem(theDialog,aButton,&itemType, &DItem, &box);
  164.         if ((itemType & 0x7F) == ctrlItem) {
  165.           SetCTitle((ControlHandle) DItem,"\pNext");
  166.         }
  167.         
  168.         GetDItem(theDialog,txtItem,&itemType, &DItem, &box);
  169.         if ((itemType & 0x7F) == statText) {
  170.           SetIText(DItem, "\pClick Next to advance frame.");
  171.         }
  172.         
  173.         GetDItem(theDialog,usrItem,&itemType, &DItem, &box);
  174.         if ((itemType & 0x7F) == userItem) {
  175.           SetDItem(theDialog,usrItem,itemType, (Handle)&myDrawPict, &box);
  176.         }
  177.         trackEnd = GetTrackDuration(videoTrack);    /* end of track in movie time        */
  178.         trackOffset = GetTrackOffset(videoTrack);         /* begining of track in movie time     */
  179.         inTime = trackOffset;
  180.         
  181.         do {
  182.           moviePict = GetTrackPict(videoTrack,inTime);
  183.           SetWRefCon(theDialog, (long)moviePict);
  184.           InvalRect(&box);
  185.  
  186.           GetTrackNextInterestingTime(videoTrack,nextTimeMediaSample,
  187.                                       inTime, forwardNormalSpeed, &inTime, nil);
  188.           if ( inTime == -1 )         /* when we ask for the next interesting time after the end     */
  189.             inTime = trackOffset;    /* the result is -1; so go to beginning.                    */
  190.             
  191.           ModalDialog(nil, &itemHit);
  192.  
  193.           KillPicture(moviePict);
  194.  
  195.         } while (itemHit != okButton);
  196.  
  197.         CloseDialog(theDialog);
  198.         SetGWorld(savePort, saveGDevice);
  199.       }
  200.     }
  201.     return;    
  202. }
  203.  
  204. /* This routine gets the poster picture and call the code that does the
  205.    actual printing. If no picture is available then it present the user
  206.    with an alert informing of the sad condition.
  207. */
  208. void PrintPoster(void)
  209. {
  210. WindowPtr window;
  211. PicHandle posterPict;
  212. DocRecHandle    wHndl;
  213. short itemHit;
  214.  
  215.     if (window = FrontWindow()) { /* don't bother if no movies */
  216.       if (IsAppWindow(window) && (wHndl = (DocRecHandle)GetWRefCon(window)) ) {
  217.         
  218.         if ( posterPict = GetMoviePosterPict((*wHndl)->wMovie)) {
  219.           PrintThis(posterPict);
  220.           KillPicture(posterPict);
  221.         }
  222.         else { /* Did not get pict bummer! */
  223.           SetCursor(&qd.arrow);
  224.           ParamText((*wHndl)->wFileInfo.sfr.sfFile.name, nil, nil, nil);
  225.           itemHit = StopAlert(noPosterAlert, nil);
  226.         }
  227.       }
  228.     }
  229.     return;    
  230. }
  231.  
  232.  
  233. /* This routine opens a dialog and displays in succession the
  234.    frames of the movie.
  235.    
  236. */
  237.  
  238. #define forwardNormalSpeed 0x00010000        /* normal speed (1) in fixed */
  239. void DoGetMoviePicts(void)
  240. {
  241. WindowPtr window;
  242. DialogPtr theDialog;
  243. PicHandle moviePict;
  244. short itemType, itemHit;
  245. Handle DItem;
  246. Rect box;
  247. CGrafPtr savePort;
  248. GDHandle saveGDevice;
  249. DocRecHandle    wHndl;
  250. TimeValue inTime;
  251. OSType trackTypes[2] = {'vide', 'text'};
  252.  
  253.     if (window = FrontWindow()) { /* don't bother if no movies */
  254.       if (IsAppWindow(window) && (wHndl = (DocRecHandle)GetWRefCon(window)) ) {
  255.         GetGWorld(&savePort, &saveGDevice);
  256.  
  257.         theDialog = GetNewDialog(pictDialog,nil, (WindowPtr) -1);
  258.         SetPort(theDialog);
  259.  
  260.         GetDItem(theDialog,aButton,&itemType, &DItem, &box);
  261.         if ((itemType & 0x7F) == ctrlItem) {
  262.           SetCTitle((ControlHandle) DItem,"\pNext");
  263.         }
  264.         
  265.         GetDItem(theDialog,txtItem,&itemType, &DItem, &box);
  266.         if ((itemType & 0x7F) == statText) {
  267.           SetIText(DItem, "\pClick Next to advance frame.");
  268.         }
  269.         
  270.         GetDItem(theDialog,usrItem,&itemType, &DItem, &box);
  271.         if ((itemType & 0x7F) == userItem) {
  272.           SetDItem(theDialog,usrItem,itemType, (Handle)&myDrawPict, &box);
  273.         }
  274.         
  275.         inTime = 0; /* movie time always starts at zero */
  276.         /* but the video track(s) could start later, like in a movie that has sound first and then
  277.            some video.
  278.            So, prime the time */
  279.         GetMovieNextInterestingTime((*wHndl)->wMovie,nextTimeMediaSample+nextTimeEdgeOK,2, trackTypes,
  280.                                       inTime, forwardNormalSpeed, &inTime, nil);
  281.         
  282.         do {
  283.           moviePict = GetMoviePict((*wHndl)->wMovie,inTime);
  284.           SetWRefCon(theDialog, (long)moviePict);
  285.           InvalRect(&box);
  286.  
  287.           GetMovieNextInterestingTime((*wHndl)->wMovie,nextTimeMediaSample,2, trackTypes,
  288.                                       inTime, forwardNormalSpeed, &inTime, nil);
  289.           if ( inTime == -1 ) {        /* when we ask for the next interesting time after the end     */
  290.             inTime = 0;                /* the result is -1; so go to beginning.                    */
  291.             GetMovieNextInterestingTime((*wHndl)->wMovie,nextTimeMediaSample+nextTimeEdgeOK,2, trackTypes,
  292.                                       inTime, forwardNormalSpeed, &inTime, nil);
  293.           }
  294.             
  295.           ModalDialog(nil, &itemHit);
  296.  
  297.           KillPicture(moviePict);
  298.  
  299.         } while (itemHit != okButton);
  300.  
  301.         CloseDialog(theDialog);
  302.         SetGWorld(savePort, saveGDevice);
  303.       }
  304.     }
  305.     return;    
  306. }
  307.